<?php
namespace jy_lianmerch;
defined('BY_JYA') or exit('error');
/*
	分销佣金
	
*/
class commission extends \plugin{
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请输入银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}
		if($post['pay_type']=='jiaoyi'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankcard']){
				exi('请输入交易所账户','error');
			}
			if(!$post['bankcard1']){
				exi('请确认交易所账户','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='交易所账户';
		}
		if($post['fee']<=0){
			exi('没有填写申请金额！','error');
		}
		if(!$post['pay_type']){
			exi('请选择提现方式！','error');
		}
		if($_SESSION['onemerchid']){
			$member=model('jy_merch/member')->getdetail($_SESSION['onemerchid'],$_SESSION['uid']);
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('credit4','credit6'));
		}
		if($post['fee_type']!=1 && $post['fee_type']!=3 && $post['fee_type']!=4 && $post['fee_type']!=6){
			exi('提现类型不支持！'.$post['fee_type'],'error');
		}
		if($post['fee_type']==1 && $post['fee']>$member['credit1']){
			exi('账户余额不足不能提现！','error');
		}
		if($post['fee_type']==3 && $post['fee']>$member['credit3']){
			exi('账户余额不足不能提现！','error');
		}
		if($post['fee_type']==4 && $post['fee']>$member['credit4']){
			exi('账户余额不足不能提现！','error');
		}
		if($post['fee_type']==4 && $post['fee']>$member['credit4']){
			exi('账户余额不足不能提现！','error');
		}
		if($post['fee_type']==6 && $post['fee']>$member['credit4']){
			exi('账户余额不足不能提现！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('balance'),'balancesn','AC'),
			'money'=>$post['fee'],
			'pay_type'=>$post['pay_type'],
			'fee_type'=>$post['fee_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'wechat'=>$post['wechat'],
			'mobile'=>$post['mobile'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$type=$post['fee_type']==4?19:20;
		if($_SESSION['onemerchid']){
			model('jy_merch/member')->member_credit($_SESSION['onemerchid'],$_SESSION['uid'],'credit'.$post['fee_type'],$new_balance['money'],false,'jy_lianmerch','提现申请');
		}else{
			model('core/member')->member_credit($_SESSION['uid'],'credit'.$post['fee_type'],$new_balance['money'],false,'jy_lianmerch','提现申请');
		}
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
		$update=array(
			'status'=>3,
			'pay_trade_no'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		if($pay_type){
			$update['pay_type']=$pay_type;
		}
		$this->update('balance',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		线下打款
	*/
	protected function pay_balance_by_downline(){
		return 'pay_trade_no';
	}
	/*
		打款到积分
	*/
	protected function pay_balance_by_credit1($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit1',$money,true,$this->plugin_name,$remark='币兑换');
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='币打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		if(!$res['payment_no']){
			exi('打款到微信零钱失败！','error');
		}
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
}